package com.cl.park.action;

import com.cl.park.model.AjaxData;
import com.cl.park.model.AjaxResponse;
import com.cl.park.model.Car;
import com.cl.park.model.Vip;
import com.cl.park.service.CarService;
import com.opensymphony.xwork2.ActionSupport;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper=false)
public class CarAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private CarService carService;
	private AjaxData<Object> data;
	private AjaxResponse result;
	private Vip vip;
	private Car car;
	private Integer id;
	
	public String editCar(){
		if(null != id){
			car = carService.getById(id);
		}
		return SUCCESS;
	}
	
	public String saveCar(){
		String res =this.carService.addOrUpdateCar(car);
		if(res.equals("ok")){
			if(null == car.getVipNo() && null != vip &&vip.getPhone() != null){
				res =this.carService.vipCar(vip, car);
				if("ok".equals(res)){
					result = new AjaxResponse(true, "操作成功");
				}else{
					result = new AjaxResponse(false, "会员信息不存在");
				}
				
			}else{
				result = new AjaxResponse(true, "操作成功");
			}
		}else{
			result = new AjaxResponse(false, "操作失败");
		}
		return SUCCESS;
	}
	
	public String listCar(){
		return SUCCESS;
	}
	
	public String getCarList(){
		data = new AjaxData<Object>(0, 0, carService.listCar(car));
		return SUCCESS;
	}
	
	public String addVipCar(){
		String res =this.carService.vipCar(vip, car);
		if(res.equals("ok")){
			result = new AjaxResponse(true, "操作成功");
		}else{
			result = new AjaxResponse(false, "会员信息不存在");
		}
		return SUCCESS;
	}
}
